package com.tinusgraglin.enclusiv.ui.components

import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActionScope
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.tinusgraglin.enclusiv.R

@Composable
fun EmailField(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier,
    textStyle: TextStyle,
    shape: Shape = RoundedCornerShape(50),
    colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors(),
    isError: Boolean = false,
    labelString: String = stringResource(R.string.ui_text_field_email),
    enabled: Boolean = true,
    onKeyBoardActionDone: (KeyboardActionScope.() -> Unit)? = null
){
    OutlinedTextField (
        value = value,
        onValueChange = { onValueChange(it) },
        modifier = modifier,
        shape = shape,
        colors = colors,
        label = { Text(labelString) },
        textStyle = textStyle,
        singleLine = true,
        isError = isError,
        enabled = enabled,
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email, imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(onDone = onKeyBoardActionDone),
    )
}

@Composable
fun PasswordField(
    value: String,
    onPasswordValueChange: (String) -> Unit,
    modifier: Modifier,
    textStyle: TextStyle,
    iconSize: Dp = 18.dp,
    shape: Shape = RoundedCornerShape(50),
    colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors(),
    isError: Boolean = false,
    labelString: String = stringResource(R.string.ui_text_field_password),
    showPasswordVisible: Boolean = true,
    enabled: Boolean = true,
    onKeyBoardActionDone: (KeyboardActionScope.() -> Unit)? = null
) {
    var showPassword by remember {
        mutableStateOf(false)
    }

    OutlinedTextField (
        value = value,
        onValueChange = {
            onPasswordValueChange(it)
        } ,
        modifier = modifier,
        shape = shape,
        colors = colors,
        textStyle = textStyle,
        singleLine = true,
        label = { Text(labelString) },
        isError = isError,
        enabled = enabled,
        visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(),
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password, imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(onDone = onKeyBoardActionDone),
        trailingIcon = if (!showPasswordVisible) null else
            { ->
                IconToggleButton (checked = showPassword, onCheckedChange = { showPassword = it }) {
                    Icon (
                        painter = painterResource(id = if (!showPassword) R.drawable.blind else R.drawable.view),
                        contentDescription = if (showPassword) "hide password" else "show password",
                        modifier = Modifier.size(iconSize)
                    )
                }
            },
    )
}
